package com.gnerv.cms.controller;

import com.gnerv.cms.dto.ArticleDTO;
import com.gnerv.cms.entity.Article;
import com.gnerv.cms.entity.Content;
import com.gnerv.cms.service.IArticleService;
import com.gnerv.cms.service.ICatalogService;
import com.gnerv.cms.service.IContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Gnerv LiGen
 * @description
 * @data 2019/7/1
 */
@Controller
public class CmsController {

    @Autowired
    ICatalogService catalogService;

    @Autowired
    IArticleService articleService;

    @Autowired
    IContentService contentService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(){
        return "cms/index";
    }

    @RequestMapping(value = "catalog/{bId}", method = RequestMethod.GET)
    public String indexCatalog(@PathVariable("bId") String bId, ModelMap modelMap){
        modelMap.addAttribute("CatalogBid", bId);
        return "cms/catalog";
    }

    @RequestMapping(value = "article/list/{cId}")
    public String listArticle(@PathVariable("cId") String cId, ModelMap modelMap){
        modelMap.addAttribute("CatalogBid", cId);
        return "cms/articleList";
    }

    @RequestMapping(value = "article/{bId}")
    public String detailArticle(@PathVariable("bId") String bId, ModelMap modelMap){
        Article article = articleService.selectArticle(bId);
        Content content = contentService.selectContentByAid(bId);
        ArticleDTO articleDTO = new ArticleDTO();
        articleDTO.setArticle(article);
        articleDTO.setContent(content);
        modelMap.addAttribute("article", articleDTO);
        return "cms/articleDetail";
    }
}
